package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.AddUserAddressModel;

public class AddAddressressActivity extends BaseActivity implements View.OnClickListener {

    EditText addaddress_username,addaddress_phone,addaddress_youbian,addaddress_address;
    Button addaddress_btn;
    String userid;
    ImageView addaddress_fh;

    TextView addaddress_xzdz;
    EditText addaddress_shengshi;

    private CityPicker cityPicker;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_add_addressress);
        addaddress_username = (EditText)findViewById(R.id.addaddress_username);
        addaddress_phone = (EditText)findViewById(R.id.addaddress_phone);
        addaddress_youbian = (EditText)findViewById(R.id.addaddress_youbian);
        addaddress_address = (EditText)findViewById(R.id.addaddress_address);
        addaddress_btn = (Button)findViewById(R.id.addaddress_btn);
        addaddress_fh = (ImageView)findViewById(R.id.addaddress_fh);

        addaddress_xzdz = (TextView)findViewById(R.id.addaddress_xzdz);
        addaddress_shengshi = (EditText)findViewById(R.id.addaddress_shengshi);

        addaddress_shengshi.setFocusable(false);
        addaddress_shengshi.setFocusableInTouchMode(false);
        addaddress_youbian.setFocusable(false);
        addaddress_youbian.setFocusableInTouchMode(false);

    }

    @Override
    void initEvent() {
        addaddress_btn.setOnClickListener(this);
        addaddress_xzdz.setOnClickListener(this);
        addaddress_fh.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.addaddress_btn:
                userid = getid();
                String username = addaddress_username.getText().toString().trim();
                String phone = addaddress_phone.getText().toString().trim();
                String youbian = addaddress_youbian.getText().toString().trim();
                String address = addaddress_address.getText().toString().trim();
                String shengshi = addaddress_shengshi.getText().toString().trim();
                if (shengshi.equals("")){
                    Toast.makeText(AddAddressressActivity.this,"请点击选择地址添加你的地址",Toast.LENGTH_SHORT).show();
                }
                else if (username.equals("") || phone.equals("")||youbian.equals("")||address.equals("")){
                    Toast.makeText(AddAddressressActivity.this,"信息不能有空",Toast.LENGTH_SHORT).show();
                }
                else if(!isMobileNO(phone)){
                    Toast.makeText(AddAddressressActivity.this,"电话号码格式错误",Toast.LENGTH_SHORT).show();
                }
                else{
                    AddUserAddressModel addUserAddressModel = new AddUserAddressModel();
                    addUserAddressModel.addUserAddress(userid,username,phone,youbian,shengshi,address,successBeanTListener);
                }
                break;
            case R.id.addaddress_xzdz:
                initCityPicker();
                cityPicker.show();
                break;
            case R.id.addaddress_fh:
                finish();
                break;


        }

    }
    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(AddAddressressActivity.this,"添加成功",Toast.LENGTH_SHORT).show();

                finish();
            }
            else{
                Toast.makeText(AddAddressressActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private static boolean isMobileNO(String mobiles) {//判断是否为手机号的正则表达式
        String telRegex = "[1][3589]\\d{9}";
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }

    public void initCityPicker() {

        //滚轮文字的大小
//滚轮文字的颜色
//省份滚轮是否循环显示
        //城市滚轮是否循环显示
//地区（县）滚轮是否循环显示
        //滚轮显示的item个数
        //滚轮item间距
        cityPicker = new CityPicker.Builder(AddAddressressActivity.this)
                .textSize(20)//滚轮文字的大小
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#0CB6CA")
                .titleTextColor("#000000")
                .backgroundPop(0xa0000000)
                .confirTextColor("#000000")
                .cancelTextColor("#000000")
                .province("xx省")
                .city("xx市")
                .district("xx区")
                .textColor(Color.parseColor("#000000"))//滚轮文字的颜色
                .provinceCyclic(true)//省份滚轮是否循环显示
                .cityCyclic(false)//城市滚轮是否循环显示
                .districtCyclic(false)//地区（县）滚轮是否循环显示
                .visibleItemsCount(7)//滚轮显示的item个数
                .itemPadding(10)//滚轮item间距
                .onlyShowProvinceAndCity(false)
                .build();

//监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //区县（如果设定了两级联动，那么该项返回空）
                String district = citySelected[2];
                //邮编
                String code = citySelected[3];

                addaddress_shengshi.setText(province + city + district);
                addaddress_youbian.setText(code);
               // Log.e("aaaaaaaaaaaaaa",adresss.getText().toString());
            }
            @Override
            public void onCancel() {


            }
        });
    }
}
